//
//  TapTableView.m
//  tableView联动
//
//  Created by 程建良 on 16/8/20.
//  Copyright © 2016年 程建良. All rights reserved.
//

#import "TapTableView.h"

@interface TapTableView()<UITableViewDataSource,UITableViewDelegate>

@end
@implementation TapTableView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tap"];
        self.showsVerticalScrollIndicator = NO;
        //接受通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(offsetChange:) name:@"offsetChange"  object:nil];
    }
    return self;
}
#pragma mark --通知实现--
-(void)offsetChange:(NSNotification*)notic{
    NSString * index = notic.object;
    [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:index.integerValue inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
#pragma mark --移除监听--
-(void)setArray:(NSArray *)array{
    _array = array;
    [self reloadData];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark -- UItableViewDataSource --
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tap"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",self.array[indexPath.row] ];
    return cell;
}
#pragma mark -- UItableViewDelegate --
//实现tap至product的联动
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"sectionChange" object:@(indexPath.row)];
}
@end
